package com.java.base.exercise.designmode.proxy.cglibproxy;

import net.bytebuddy.build.BuildLogger;
import net.sf.cglib.proxy.*;

import java.lang.reflect.Method;

/**
 * @Author RenPu
 * @Date 2022/6/20 17:00
 * @Version 1.0
 * @Description: Cglib对象的工厂类
 **/
public class CglibProxyFactory {

    public static Object getProxyObj(Class<?> clzz){
        //创建动态代理增强类
        Enhancer enhancer = new Enhancer();

        //设置classLoader
        enhancer.setClassLoader(clzz.getClassLoader());

        //设置被代理的类
        enhancer.setSuperclass(clzz);

        //设置执行的拦截器 new CglibMethodInterceptor()
        enhancer.setCallbacks(new Callback[]{new CglibMethodInterceptor(),
                NoOp.INSTANCE});

        enhancer.setCallbackFilter(new FilterImplement());
        //返回对象
        return enhancer.create();

    }


}
